{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "内容相同的文件：\n",
      "D:\\mmmmmm\\aaaaa.txt\n",
      "D:\\mmmmmm\\gggg.txt\n",
      "内容相同的文件：\n",
      "D:\\mmmmmm\\fffff.txt\n",
      "D:\\mmmmmm\\yyyy.txt\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "def find_duplicate_files(folder_path):\n",
    "    # 创建一个字典来存储文件内容及其对应的文件名\n",
    "    file_contents = {}\n",
    "\n",
    "    # 遍历文件夹中的所有文件\n",
    "    for root, dirs, files in os.walk(folder_path):\n",
    "        for file in files:\n",
    "            file_path = os.path.join(root, file)\n",
    "            # 读取文件内容\n",
    "            with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:                                                          马东升\n",
    "                content = f.read()\n",
    "            # 将文件内容作为键，文件名作为值存储在字典中\n",
    "            if content in file_contents:\n",
    "                file_contents[content].append(file_path)\n",
    "            else:\n",
    "                file_contents[content] = [file_path]\n",
    "\n",
    "    # 找出内容相同的文件\n",
    "    duplicate_files = {content: filenames for content, filenames in file_contents.items() if len(filenames) > 1}\n",
    "\n",
    "    return duplicate_files\n",
    "\n",
    "# 指定文件夹路径\n",
    "folder_path = r'D:\\mmmmmm'\n",
    "\n",
    "# 调用函数并打印结果\n",
    "duplicate_files = find_duplicate_files(folder_path)\n",
    "for content, filenames in duplicate_files.items():\n",
    "    print(f\"内容相同的文件：\")\n",
    "    for filename in filenames:\n",
    "        print(filename)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
